package com.haibei.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

import com.haibei.R;

public class CircleFlowIndicator extends View implements FlowIndicator {
	private float radius = 10;
	private final Paint mPaintStroke = new Paint(Paint.ANTI_ALIAS_FLAG);
	private final Paint mPaintFill = new Paint(Paint.ANTI_ALIAS_FLAG);
	private ViewFlow viewFlow;
	private int currentScroll = 0;
	private int flowWidth = 0;

	private int distance = 30;
	private int postion;

	private PicSwitchListener mSwitchListener;

	public CircleFlowIndicator(Context context) {
		super(context);
		initColors(0xFFFFFFFF, 0xFFFFFFFF);
	}

	public CircleFlowIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleFlowIndicator);
		int fillColor = a.getColor(R.styleable.CircleFlowIndicator_fillColor, 0xff46b5c2);// #46b5c2
		int strokeColor = a.getColor(R.styleable.CircleFlowIndicator_strokeColor, 0xFFFFFFFF);
		radius = a.getDimension(R.styleable.CircleFlowIndicator_radius, 5.0f);
		initColors(fillColor, strokeColor);
	}

	private void initColors(int fillColor, int strokeColor) {
		mPaintStroke.setStyle(Style.STROKE);
		mPaintStroke.setColor(strokeColor);
		mPaintFill.setStyle(Style.FILL);
		mPaintFill.setColor(fillColor);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int count = 3;
		if (viewFlow != null) {
			count = viewFlow.getViewsCount();
		}
		int w=getWidth();
		for (int iLoop = 0; iLoop < count; iLoop++) {
			mPaintStroke.setStyle(Style.FILL);
			mPaintStroke.setColor(0x885b5b5b);
			canvas.drawCircle(distance + getPaddingLeft() + radius + (iLoop * (2 * radius + radius)), getPaddingTop() + radius, radius,
					mPaintStroke);
		}
		float cx = 0;
		if (flowWidth != 0) {
			cx = (currentScroll * (2 * radius + radius)) / flowWidth;
		}
		canvas.drawCircle(distance + getPaddingLeft() + radius + cx, getPaddingTop() + radius, radius, mPaintFill);
	}

	@Override
	public void onSwitched(View view, int position) {
		postion = position;
		if (null != mSwitchListener) {
			mSwitchListener.onSwith(position);
		}
	}

	@Override
	public void setViewFlow(ViewFlow view) {
		viewFlow = view;
		flowWidth = viewFlow.getWidth();
		invalidate();
	}

	@Override
	public void onScrolled(int h, int v, int oldh, int oldv) {
		currentScroll = h;
		flowWidth = viewFlow.getWidth();
		invalidate();
	}

	public PicSwitchListener getmSwitchListener() {
		return mSwitchListener;
	}

	public void setmSwitchListener(PicSwitchListener mSwitchListener) {
		this.mSwitchListener = mSwitchListener;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
	}

	/**
	 * Determines the width of this view
	 * 
	 * @param measureSpec
	 *            A measureSpec packed into an int
	 * @return The width of the view, honoring constraints from measureSpec
	 */
	private int measureWidth(int measureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		// We were told how big to be
		if (specMode == MeasureSpec.EXACTLY) {
			result = specSize;
		}
		// Calculate the width according the views count
		else {
			int count = 3;
			if (viewFlow != null) {
				count = viewFlow.getViewsCount();
			}
			result = (int) (getPaddingLeft() + getPaddingRight() + (count * 2 * radius) + (count - 1) * radius + 1);
			// Respect AT_MOST value if that was what is called for by
			// measureSpec
			if (specMode == MeasureSpec.AT_MOST) {
				result = Math.min(result, specSize);
			}
		}
		return result;
	}

	/**
	 * Determines the height of this view
	 * 
	 * @param measureSpec
	 *            A measureSpec packed into an int
	 * @return The height of the view, honoring constraints from measureSpec
	 */
	private int measureHeight(int measureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		// We were told how big to be
		if (specMode == MeasureSpec.EXACTLY) {
			result = specSize;
		}
		// Measure the height
		else {
			result = (int) (2 * radius + getPaddingTop() + getPaddingBottom() + 1);
			// Respect AT_MOST value if that was what is called for by
			// measureSpec
			if (specMode == MeasureSpec.AT_MOST) {
				result = Math.min(result, specSize);
			}
		}
		return result;
	}

	/**
	 * Sets the fill color
	 * 
	 * @param color
	 *            ARGB value for the text
	 */
	public void setFillColor(int color) {
		mPaintFill.setColor(color);
		invalidate();
	}

	/**
	 * Sets the stroke color
	 * 
	 * @param color
	 *            ARGB value for the text
	 */
	public void setStrokeColor(int color) {
		mPaintStroke.setColor(color);
		invalidate();
	}

	public static interface PicSwitchListener {
		public void onSwith(int position);
	};
}